package com.zzptc.syl.traffic;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class WeatherFragment extends Fragment {
    private int k=0;
    private LifeIndex data;
    List<BarEntry> barEntries =new ArrayList<>();
    private BarChart bar;
    private View view;
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_weather, container, false);
        bar = (BarChart) view.findViewById(R.id.barChart);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                sport();
            }
        },0,3000);
        return view;
    }
    private void sport() {
        Service service=retrofit.create(Service.class);
        Call<LifeIndex> life=service.life();
        life.enqueue(new Callback<LifeIndex>() {
            @Override
            public void onResponse(Call<LifeIndex> call, Response<LifeIndex> response) {
                data=response.body();
                k++;
                barEntries.add(new BarEntry(k,data.get温度()));
                cylinder();
            }
            @Override
            public void onFailure(Call<LifeIndex> call, Throwable t) {

            }
        });
    }
    private void cylinder() {
        //添加数据
//        barEntries.add(new BarEntry(3,88));
//        barEntries.add(new BarEntry(6,92));
//        barEntries.add(new BarEntry(9,96));
//        barEntries.add(new BarEntry(12,103));
//        barEntries.add(new BarEntry(15,105));
//        barEntries.add(new BarEntry(18,103));
//        barEntries.add(new BarEntry(21,101));
        BarDataSet barDataSet=new BarDataSet(barEntries,"");
        BarData barData=new BarData(barDataSet);
        bar.setData(barData);
        XAxis x = bar.getXAxis();
        bar.getXAxis().setDrawGridLines(false);  //是否绘制X轴上的网格线（背景里面的竖线）
        bar.getDescription().setEnabled(false);//隐藏右下角英文
        bar.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);//X轴的位置 默认为上面
        bar.getAxisRight().setEnabled(false);//隐藏右侧Y轴   默认是左右两侧都有Y轴
        bar.invalidate();
        Legend legend=bar.getLegend();
        legend.setEnabled(false);    //是否显示图例
//        x.setValueFormatter(new IAxisValueFormatter() {   //X轴自定义坐标
//            @Override
//            public String getFormattedValue(float v, AxisBase axisBase) {
//                for (int b=0;b<60;b++){
//                    if (b+3==v ){
//                        return ""+b;
//                    }
//                }return "";//注意这里需要改成 ""
//            }
//        });
        x.setAxisMaximum(20);   //X轴最大数值
        x.setAxisMinimum(0);   //X轴最小数值
        bar.invalidate();
    }
}